package com.egao.orderB.biz.impl;

import com.egao.common.core.web.BaseDao;
import com.egao.common.core.web.JsonResult;
import com.egao.common.core.web.PageResult2;
import com.egao.common.core.utils.Utils;
import com.egao.common.system.entity.User;
import com.egao.common.core.constants.UserConstant;
import com.egao.orderB.biz.MaintainFeeAttachBiz;
import com.egao.orderB.entity.MaintainFeeAttach;
import net.sf.json.JSONObject;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;
import java.util.List;

/**
 *
 * 数据层实现类
 * @author : Doman
 * @date : 2020-06-15 15:18:10
 * @version ：1.0
 */
@Repository
public class MaintainFeeAttachBizImpl extends BaseDao implements MaintainFeeAttachBiz {

	@Override
	public JsonResult list(PageResult2<T> page, MaintainFeeAttach maintainFeeAttach, User loginUser) throws Exception {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<MaintainFeeAttach> rm = new BeanPropertyRowMapper<MaintainFeeAttach>(MaintainFeeAttach.class);
		StringBuffer sql = new StringBuffer("select m.id,m.company_id,m.name");
        sql.append(",m.standard,m.box_type,m.price,m.type,m.remark");
		sql.append(",m.create_time,m.create_user,m.update_time,m.update_user");
        sql.append(",cmy.name companyName,suser.username createName ");
        sql.append(" from chf_maintain_fee_attach m ");
        sql.append(" inner join sys_user suser on suser.user_id=m.create_user ");
        sql.append(" inner join chf_company cmy on cmy.id=m.company_id ");
        sql.append(" where m.deleted=0 and cmy.deleted=0 ");
		if(loginUser.getUserId() != UserConstant.ADMIN_ID){
            sql.append(" and m.company_id=:tablecompanyId ");
            map.put("tablecompanyId", loginUser.getCompanyId() + "");
        }
    	if(Utils.isNotEmpty(maintainFeeAttach)){
			if(Utils.isNotEmpty(maintainFeeAttach.getName())){
				sql.append(" and instr(m.name,:name)>0 ");
				map.put("name", maintainFeeAttach.getName());
			}
			if(Utils.isNotEmpty(maintainFeeAttach.getStandard())){
				sql.append(" and instr(m.standard,:standard)>0 ");
				map.put("standard", maintainFeeAttach.getStandard());
			}
			if(Utils.isNotEmpty(maintainFeeAttach.getBoxType())){
				sql.append(" and instr(m.box_type,:box_type)>0 ");
				map.put("box_type", maintainFeeAttach.getBoxType());
			}
			if(Utils.isNotEmpty(maintainFeeAttach.getType())){
				sql.append(" and instr(m.type,:type)>0 ");
				map.put("type", maintainFeeAttach.getType());
			}
			if(Utils.isNotEmpty(maintainFeeAttach.getSort())&&Utils.isNotEmpty(maintainFeeAttach.getOrder())){
				sql.append(" order by "+maintainFeeAttach.getSort()+" "+maintainFeeAttach.getOrder());
			}
		}
		return jsonResult(getPageQueryList(page, sql.toString(), map, rm));
	}

	@Override
	public List<MaintainFeeAttach> list(MaintainFeeAttach maintainFeeAttach, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<MaintainFeeAttach> rm = new BeanPropertyRowMapper<MaintainFeeAttach>(MaintainFeeAttach.class);
        StringBuffer sql = new StringBuffer("select m.id,m.company_id,m.name");
        sql.append(",m.standard,m.box_type,m.price,m.type,m.remark");
		sql.append(",m.create_time,m.create_user,m.update_time,m.update_user");
        sql.append(",cmy.name companyName,suser.username createName ");
        sql.append(" from chf_maintain_fee_attach m ");
        sql.append(" inner join sys_user suser on suser.user_id=m.create_user ");
        sql.append(" inner join chf_company cmy on cmy.id=m.company_id ");
        sql.append(" where m.deleted=0 and cmy.deleted=0 ");
		if(loginUser.getUserId() != UserConstant.ADMIN_ID){
            sql.append(" and m.company_id=:tablecompanyId ");
            map.put("tablecompanyId", loginUser.getCompanyId() + "");
        }
    	if(Utils.isNotEmpty(maintainFeeAttach)){
			if(Utils.isNotEmpty(maintainFeeAttach.getName())){
				sql.append(" and instr(m.name,:name)>0 ");
				map.put("name", maintainFeeAttach.getName());
			}
			if(Utils.isNotEmpty(maintainFeeAttach.getStandard())){
				sql.append(" and instr(m.standard,:standard)>0 ");
				map.put("standard", maintainFeeAttach.getStandard());
			}
			if(Utils.isNotEmpty(maintainFeeAttach.getBoxType())){
				sql.append(" and instr(m.box_type,:box_type)>0 ");
				map.put("box_type", maintainFeeAttach.getBoxType());
			}
			if(Utils.isNotEmpty(maintainFeeAttach.getType())){
				sql.append(" and instr(m.type,:type)>0 ");
				map.put("type", maintainFeeAttach.getType());
			}
		}
		getMapParameter(sql, map);
		return jdbcTemplate.query(sql.toString(),map, rm);
	}

	@Override
	public MaintainFeeAttach getById(MaintainFeeAttach maintainFeeAttach) {
		RowMapper<MaintainFeeAttach> rm = new BeanPropertyRowMapper<MaintainFeeAttach>(MaintainFeeAttach.class);
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql = new StringBuffer("select m.id,m.company_id,m.name");
        sql.append(",m.standard,m.box_type,m.price,m.type,m.remark");
		sql.append(",m.create_time,m.create_user,m.update_time,m.update_user");
        sql.append(",cmy.name companyName,suser.username createName ");
        sql.append(" from chf_maintain_fee_attach m ");
        sql.append(" inner join sys_user suser on suser.user_id=m.create_user ");
        sql.append(" inner join chf_company cmy on cmy.id=m.company_id ");
        sql.append(" where m.deleted=0 and cmy.deleted=0 and m.id=:id ");
		map.put("id", maintainFeeAttach.getId());
		getMapParameter(sql, map);
		return jdbcTemplate.queryForObject(sql.toString(), map, rm);
	}

	@Override
	public boolean save(MaintainFeeAttach maintainFeeAttach, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("insert into chf_maintain_fee_attach (");
        sql.append("company_id,name,standard,box_type");
		sql.append(",price,type,remark,create_user,create_time) values ( ");
        sql.append(":company_id,:name,:standard,:box_type");
		sql.append(",:price,:type,:remark,:create_user,now())");
        map.put("company_id", loginUser.getCompanyId() + "");
        map.put("name", maintainFeeAttach.getName());
        map.put("standard", maintainFeeAttach.getStandard());
        map.put("box_type", maintainFeeAttach.getBoxType());
        map.put("price", maintainFeeAttach.getPrice());
        map.put("type", maintainFeeAttach.getType());
        map.put("remark", maintainFeeAttach.getRemark());
		map.put("create_user", loginUser.getUserId() + "");
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean update(MaintainFeeAttach maintainFeeAttach, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("update chf_maintain_fee_attach set ");
        sql.append("name=:name,standard=:standard,box_type=:box_type ");
		sql.append(",price=:price,remark=:remark,update_user=:update_user where id=:id ");
        map.put("name", maintainFeeAttach.getName());
        map.put("standard", maintainFeeAttach.getStandard());
        map.put("box_type", maintainFeeAttach.getBoxType());
        map.put("price", maintainFeeAttach.getPrice());
        map.put("remark", maintainFeeAttach.getRemark());
		map.put("update_user", loginUser.getUserId() + "");
		map.put("id", maintainFeeAttach.getId());
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean remove(MaintainFeeAttach maintainFeeAttach, User loginUser) {
		int count = 0;
		Map<String, String> map = null;
		if(maintainFeeAttach.getId().length()>0){
			for(String ID : maintainFeeAttach.getId().split(",")){
				map  = new HashMap<String, String>();
				if(ID==null || ID.equals("")){ break; }
				StringBuffer sql =new StringBuffer("update chf_maintain_fee_attach set deleted=1" +
						",update_user=:update_user, update_time=now() "
						+ " where id =:id ");
				map.put("update_user", loginUser.getUserId() + "");
				map.put("id", ID);
				getMapParameter(sql, map);
				count = jdbcTemplate.update(sql.toString(),map);
			}
		}
		return count > 0;
	}
}
